package com.zzyl.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author byZhao
 * @create 2025/6/26 15:54
 * @description
 */
@Slf4j
public class PDFUtil {
    public static String pdfToString(InputStream inputStream) {
        // try.....with...resource java1.7 语法糖解决资源释放代码臃肿
        try (PDDocument document = Loader.loadPDF(inputStream.readAllBytes())) {
            PDFTextStripper pdfText = new PDFTextStripper();
            return pdfText.getText(document);
        } catch (Exception e) {
            log.error("解析PDF文件异常异常问题:{}", e.getMessage());
        }
        return null;
    }
}
